package com.wltt;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public abstract class MailSendUtil {


    /**
     * 发送邮件
     *
     * @param host         host
     * @param port         端口
     * @param sslEnable    是否开启ssl认证
     * @param mailAccount  发送邮箱账户
     * @param mailPassword 授权码或者密码
     * @param mailTitle    邮件标题
     * @param mailContent  邮件内容
     * @param receiveMails 接收邮箱集合
     * @return 返回Base64转码后的加密数据
     */
    public static void sendMail(String host, Integer port,
                                Boolean sslEnable,
                                String mailAccount, String mailPassword,
                                String mailTitle,
                                String mailContent,
                                List<String> receiveMails) throws MessagingException {
        Transport transport = null;
        try {
            // 创建会话
            Properties properties = new Properties();
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.host", host);

//        MailSSLSocketFactory sf = new MailSSLSocketFactory();
//        sf.setTrustAllHosts(true);
//        properties.put("mail.smtp.ssl.socketFactory", sf);
            if (sslEnable) {
                properties.put("mail.smtp.ssl.enable", "true");
            }
            if (port != null) {
                properties.setProperty("mail.smtp.port", "465");
                properties.setProperty("mail.smtp.socketFactory.port", "465");
            }
//            Session session = Session.getDefaultInstance(properties, new Authenticator() {
//                @Override
//                protected PasswordAuthentication getPasswordAuthentication() {
//                    return new PasswordAuthentication(mailAccount, mailPassword);
//                }
//            });
            Session session = Session.getInstance(properties);
        session.setDebug(true);
            transport = session.getTransport();
            transport.connect(mailAccount, mailPassword);

            // 创建邮件
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(new InternetAddress(mailAccount));
            mimeMessage.setSentDate(new Date());
            mimeMessage.setSubject(mailTitle, "UTF-8");
            mimeMessage.setContent(mailContent, "text/html;charset=UTF-8");

            // 设置接受者
            List<InternetAddress> internetAddressList = new ArrayList<>();
            for (String receiveMail : receiveMails) {
                internetAddressList.add(new InternetAddress(receiveMail));
            }
            InternetAddress[] internetAddresses = internetAddressList.stream().toArray(InternetAddress[]::new);
            mimeMessage.addRecipients(Message.RecipientType.TO, internetAddresses);

            // 发送邮件
            transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

        } finally {
            if (transport != null) {
                transport.close();
            }
        }
    }
}
